package com.basic.aqs;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ReetrentLockTest extends AbstractQueuedSynchronizer {

	private static final long serialVersionUID = 7577788437926810866L;

	private static final ReentrantLock lock = new ReentrantLock();

	private static final Condition c1 = lock.newCondition();

	public static void main(String[] args) throws Exception {
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(5);
					lock.lock();
					System.out.println("t .....");
					c1.signalAll();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					System.out.println("t unlock");
					lock.unlock();
				}
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				try {
					lock.lock();
					System.out.println("t2 .....");
//					c1.signalAll();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					System.out.println("t2 unlock");
					lock.unlock();
				}
			}
		}).start();
		try {
			lock.lock();
			System.out.println("main await....");
			c1.await();
		} finally {
			lock.unlock();
		}
		System.out.println("main .....");
	}
}
